package com.thoughtworks.kontinuum.grabandmove;

import java.io.File;
import java.util.Date;

import com.thoughtworks.kontinuum.constants.Messages;
import com.thoughtworks.kontinuum.exception.KonException;

public class GrabAndMove {
	
	private static final String DESTINATION_PREFIX = "RESULT";
	private String originDir;
	
	private String destinationDir;
	
	public GrabAndMove(String originDir, String destinationDir) throws KonException, Exception{
		this.originDir = originDir;
		this.destinationDir = destinationDir;
		if(!(new File(originDir)).isDirectory()){
			throw new KonException(originDir+" is not a valid directory.");
		}
		if(!(new File(destinationDir)).isDirectory()){
			throw new KonException(originDir+" is not a valid directory.");
		}
		
	}
	
	/**
	 * Store files on final destination.
	 * Throws Exception if error occurs.
	 * @return
	 */
	public void doMove() throws KonException{
		try{
			File originFile = new File(this.originDir); 
			 
			if( originFile.isDirectory()){
				originFile.renameTo(new File(getNewName()));
			} else {
				//TODO complete
				throw new KonException(Messages.ERROR_INVALID_DIR_NAME);
			}
		} catch(Exception e){
			throw new KonException(Messages.ERROR_INVALID_DIR_NAME);
		}
	}
	
	public String getNewName(){
		return destinationDir+"/"+DESTINATION_PREFIX+(new Date()).toString();
	}
	
}
